package game.seungha;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Listener_NumberButton implements OnClickListener  
{
	final int INDEX_1ST			= 0;
	final int NUMBER_1ST		= 1;	
	
	boolean		isCorrect[];
	
	Button 		mButton[];
	
	Activity_Play 	mActivity;
	
	Listener_NumberButton(Activity_Play activity)
	{
		Log.i("DBG", "[Listener_NumberButton]Listener_NumberButton"+" activity:"+activity);
		
		mActivity 	= activity;			
	}
	
	public void setButtonArray(Button numButton[])
	{
		Log.i("DBG", "[Listener_NumberButton]setButtonArray"+" numButton.length:"+numButton.length);
		
		if(numButton.length < 1)
		{// numButton이 유효하지 않다면 처리 안함.
			return;
		}
		
		mButton 	= numButton;
		isCorrect	= new boolean[numButton.length];
	}
	
	@Override
	public void onClick(View v)
	{
		int clickNumber;
		int lastIndex;
		
		boolean bRes = false;
		
		Button clickButton;
		
		Log.i("DBG", "[Listener_NumberButton]onClick"+" v.getId:"+v.getId());
		
		if(mButton.length < 1)
		{// setButtonArray()가 지정이 안된 상태라면 처리 안함.
			Log.i("DBG", "[Listener_NumberButton]onClick"+" mButton.length:"+mButton.length);
			return;
		}
		
		clickButton = (Button) v;
		clickNumber	= Integer.valueOf((String) clickButton.getText());
		
		lastIndex	= mButton.length-1;
		
		if(isCorrect[INDEX_1ST] == false)
		{
			bRes = this.click_First(clickButton, clickNumber);
		}
		else
		{
			if( (isCorrect[lastIndex-1]==true) && (isCorrect[lastIndex]==false) )
			{
				bRes = this.click_Last(clickButton, clickNumber);
			}
			else
			{
				bRes = this.click_Normal(clickButton, clickNumber);
			}
		}
		
		if(bRes == false)
		{
			NGInfo.mIncorrect++;
			this.mActivity.incorrectView_Draw();
		}
		
		
		

	}
	
	private boolean click_First(Button clickButton, int clickNumber)
	{
		boolean bRes = false;
		
		int clickIndex = clickNumber-1;
		
		Log.i("DBG", "[Listener_NumberButton]click_First"+" clickNumber:"+clickNumber);
		
		if(clickNumber == NUMBER_1ST)
		{
			isCorrect[clickIndex] = true;
			clickButton.setEnabled(false);
			
			for(Button allButton : mButton)
			{
				//Click된 1Button은 지우지 않는다.
				if(allButton.equals(clickButton) == false)allButton.setTextSize((float)0.1);
			}
			bRes = true; //제대로 처리가 된 경우
		}
		
		return bRes;
	}
	
	private boolean click_Normal(Button clickButton, int clickNumber)
	{
		boolean bRes = false;
		
		int clickIndex;
		int prevIndex;
		
		if(clickNumber==NUMBER_1ST)return bRes;

		clickIndex	= clickNumber-1;
		prevIndex	= clickIndex-1;
		
		if( (isCorrect[prevIndex]==true) && (isCorrect[clickIndex]==false) )
		{
			isCorrect[clickIndex] = true;
			clickButton.setEnabled(false);
			clickButton.setTextSize(NG.TextSize.NUMBER_BUTTON);
			
			bRes = true; //제대로 처리가 된 경우
		}
		
		return bRes;
	}
		
	private boolean click_Last(Button clickButton, int clickNumber)
	{
		boolean bRes = false;
		
		bRes = this.click_Normal(clickButton, clickNumber);
		if(bRes == false)return bRes;
		
		mActivity.mTimer.cancel();
		NGInfo.mTime_Finish 	= System.currentTimeMillis();

		Intent intent	= new Intent(mActivity, Activity_Result.class);
		//mActivity.startActivity(intent);
		mActivity.startActivityForResult(intent, 100);
		//mActivity.finish();
		
		return bRes;
	}
	
	
	
	

}
